﻿@page
@model LogNet6.Sqlite.Demo.Pages.Log.IndexModel
@{
    ViewData["Title"] = "日志列表";
}
<form id="Form1">
    <input asp-for="RemoveId" type="hidden" />
    <div class="input-group">

        <input class="btn btn-outline-secondary" type="button" value="前一页" onclick="SetPage(-1)" />
        <span class="input-group-text">第</span>
        <input type="text" class="form-control text-center" asp-for="PageNo" />
        <span class="input-group-text">页</span>
        <input type="text" class="form-control text-center" asp-for="PageSize" />
        <span class="input-group-text">条/页</span>
        <input class="btn btn-outline-secondary" type="button" value="后一页" onclick="SetPage(1)" />
    </div>
</form>

<table class="table">
    <thead>
        <tr>
            <th>#</th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].DateTime)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].LogLevel)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].CategoryName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Items[0].Message)
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Items)
        {
            string trclass;
            switch (item.LogLevel)
            {
                case "Error":
                    trclass = "table-danger";
                    break;
                case "Warning":
                    trclass = "table-warning";
                    break;
                default:
                    trclass = "table-info";
                    break;
            }

            <tr class="@trclass">
                <td>
                    <a href="javascript:;" onclick="Remove(-@item.Id)" title="删除后面所有的日志">
                        @Html.DisplayFor(modelItem => item.Id)
                    </a>
                </td>
                <td>
                    <a href="javascript:;" onclick="Remove(@item.Id)" title="删除这条日志">
                        @Html.DisplayFor(modelItem => item.DateTime)
                    </a>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LogLevel)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CategoryName)
                </td>
                <td style="word-break:break-all;word-wrap:break-word">
                    @Html.Raw(item.Message.Replace("\r\n","<br/>"))
                </td>
            </tr>
        }
    </tbody>
</table>
@section scripts
    {
    <script>
        $(function () {
            $("#PageNo").change(function () {
                $("#Form1").submit();
            });
            $("#PageSize").change(function () {
                $("#Form1").submit();
            });

        });
        function SetPage(count) {
            var pageNo = $("#PageNo").val() * 1 + count;
            if (pageNo < 0)
                return;
            $("#PageNo").val(pageNo);
            $("#Form1").submit();
        }
        function Remove(id) {
            var msg = "您确定要删除这条日志吗？"
            if (id < 0)
                msg = "您确定删除后面所有的日志吗？"
            if (confirm(msg)) {
                $("#RemoveId").val(id);
                $("#Form1").submit();
            }
        }
    </script>
}